//
//  WenBaTagListController.m
//  suRead
//
//  Created by nancy  on 16/9/12.
//  Copyright © 2016年 bao. All rights reserved.
//

#import "WenBaTagListController.h"
#import "Masonry.h"
#import "MJRefresh.h"
#import "UIViewController+HUD.h"
#import "UIImageView+WebCache.h"
#import "ServiceNews.h"

#import "M_News.h"

#import "NewsQuestionCell.h"


#import "NewsDetailViewController.h"
#import "NewsAlbumViewController.h"
#import "NewsWebViewController.h"
#import "WenBaDetailController.h"

#define ScreenRectHeight [UIScreen mainScreen].applicationFrame.size.height
#define ScreenRectWidth [UIScreen mainScreen].applicationFrame.size.width

@interface WenBaTagListController ()<UITableViewDataSource,UITableViewDelegate,NewsQuestionCellDelegate>

@property (nonatomic,strong)NSMutableArray *listNews;
@property (weak, nonatomic) IBOutlet UINavigationItem *mNavigationBar;

@property (nonatomic,strong) IBOutlet UITableView *newsTableView;
@property (nonatomic,strong)NSURLSessionDataTask *requestHandle;
@property (nonatomic,assign)NSInteger currentPageIndex;


@end

@implementation WenBaTagListController

- (IBAction)back:(id)sender
{
    //返回
    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    __weak typeof(self) weak_self = self;
    //    self.newsTableView.contentInset = UIEdgeInsetsMake(-20.0f, 0.0f, 0.0f, 0.0);
    self.newsTableView.delegate = self;
    self.newsTableView.dataSource = self;
    self.newsTableView.tableFooterView = [UIView new];
    self.newsTableView.backgroundColor = [[UIColor alloc] initWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    self.newsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.newsTableView registerNib:[UINib nibWithNibName:@"NewsQuestionCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NewsQuestionCell"];
    
    self.newsTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //刷新数据
        [weak_self refreshData];
    }];
    
    self.newsTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weak_self loadMore];
    }];

    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.newsTableView.mj_header beginRefreshing];
    });
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
}

- (void)refreshData
{
    self.currentPageIndex = 1;
    if (self.requestHandle) {
        [self.requestHandle cancel];
        self.requestHandle = nil;
    }
    
    
    __weak typeof(self) weak_self = self;
    [ServiceNews doFetchWenBaCtgNewsList:self.subURL pageIndex:self.currentPageIndex pageSize:DEFAULT_PAGE_SIZE success:^(id responseObject, NSString *message) {
        [weak_self.newsTableView.mj_header endRefreshing];
        NSString *title= [responseObject objectForKey:@"title"];
        self.mNavigationBar.title=title;
        NSArray *listNews_ = [responseObject objectForKey:@"list"];
        weak_self.listNews = [NSMutableArray array];
        if (listNews_) {
            [weak_self.listNews addObjectsFromArray:listNews_];
        }
        [weak_self.newsTableView reloadData];
        if ([listNews_ count]<DEFAULT_PAGE_SIZE) {
            [weak_self.newsTableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSString *message) {
        [weak_self.newsTableView.mj_header endRefreshing];
        
    }];
}

- (void)loadMore
{
    self.currentPageIndex ++;
    if (self.requestHandle) {
        [self.requestHandle cancel];
        self.requestHandle = nil;
    }
    
    __weak typeof(self) weak_self = self;
    [ServiceNews doFetchWenBaCtgNewsList:self.subURL pageIndex:self.currentPageIndex pageSize:DEFAULT_PAGE_SIZE success:^(id responseObject, NSString *message) {
        [weak_self.newsTableView.mj_header endRefreshing];
        NSString *title= [responseObject objectForKey:@"title"];
        self.mNavigationBar.title=title;
        NSArray *listNews_ = [responseObject objectForKey:@"list"];
        if (listNews_) {
            [weak_self.listNews addObjectsFromArray:listNews_];
        }
        [weak_self.newsTableView reloadData];
        if ([listNews_ count]<DEFAULT_PAGE_SIZE) {
            [weak_self.newsTableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSString *message) {
        [weak_self.newsTableView.mj_header endRefreshing];
        
    }];
}

- (void)jumpToNewsDetail:(M_News *)news_
{
    //去问吧详情
    //问吧详情
    WenBaDetailController *controller = [WenBaDetailController new];
    controller.hidesBottomBarWhenPushed = YES;
    controller.subURL=news_.f_url;
    if ( [[[UIDevice currentDevice]systemVersion] floatValue] >= 8.0) {
        [self showViewController:controller sender:self];
    }else{
        [self.navigationController pushViewController:controller animated:YES];
    }
}

#pragma mark tableViewDatasouce Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (!section) {
        
       return 0;
    }
    else {
        return [self.listNews count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //问吧
    M_News *news = [self.listNews objectAtIndex:indexPath.row];
    NewsQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsQuestionCell"];
    cell.newsQuestionCellDelegate=self;
    cell.newsData = news;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return 251.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section) {
        M_News *news_ = [self.listNews objectAtIndex:indexPath.row];
        [self jumpToNewsDetail:news_];
    }
}

-(void) toWenBaCtgListPage:(NSString *) ctgUrl{
    //去问吧专题界面
    //专题
    WenBaTagListController *controller = [WenBaTagListController new];
    controller.hidesBottomBarWhenPushed = YES;
    controller.subURL =ctgUrl;
    [self.navigationController pushViewController:controller animated:YES];
}

@end
